package com.kly.order.serviceImpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.fqgj.common.api.Response;
import com.kly.dto.ProductCategoryDto;
import com.kly.dto.ProductConfig;
import com.kly.dto.ProductDescriptionDto;
import com.kly.enums.TradeResultStatus;
import com.kly.order.ServiceUtils;
import com.kly.order.db.dao.ProductBenefitsDao;
import com.kly.order.db.dao.ProductCategoryDao;
import com.kly.order.db.dao.ProductDao;
import com.kly.order.db.entity.ProductBenefitsEntity;
import com.kly.order.db.entity.ProductCategoryEntity;
import com.kly.order.db.entity.ProductEntity;
import com.kly.order.mapper.ProductCategoryMapper;
import com.kly.service.ProductCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("productCategoryService")
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Resource
    ProductCategoryDao productCategoryDao;

    @Resource
    ProductBenefitsDao productBenefitsDao;

    @Resource
    ProductDao productDao;

    @Resource
    ProductCategoryMapper productCategoryMapper;

    @Resource
    ProductDescriptionAndSubscriptionService PDSService;

//    /**
//     * @param appCode         应用码
//     * @param productCategory 产品类别
//     * @return 产品类别全产品消息
//     */
//    @Override
//    public Response<ProductCategoryDto> queryProductCategory(String appCode, Integer productCategory) {
//        Map<String, Object> params = ServiceUtils.getParams();
//        params.put("productCategory", productCategory);
//        //查询产品类别消息
//        List<ProductCategoryEntity> categoryList = productCategoryDao.selectByParams(params);
//        if (CollUtil.isNotEmpty(categoryList)) {
//            ProductCategoryEntity productCategoryEntity = categoryList.get(0);
//            ProductCategoryDto productCategoryDto = productCategoryMapper.toDto(productCategoryEntity);
//
//            //查询产品类别下全产品消息
//            List<ProductEntity> productEntities = productDao.selectByProductCategory(appCode, productCategory);
//
//            List<ProductDescriptionDto> ProductDescriptions = new ArrayList<>();
//            for (ProductEntity product : productEntities) {
//                ProductDescriptionDto productDescription =
//                        JSONUtil.toBean(product.getDescription(), ProductDescriptionDto.class);
//                productDescription.setProductCode(product.getProductCode());
//                ProductDescriptions.add(productDescription);
//            }
//
//            List<ProductBenefitsEntity> productBenefitsEntities = productBenefitsDao.selectByParams(params);
//            List<ProductCategoryDto.Subscriptions> subscriptionList = new ArrayList<>();
//            if (CollUtil.isNotEmpty(productBenefitsEntities)) {
//                for (ProductBenefitsEntity productBenefit: productBenefitsEntities){
//                    ProductCategoryDto.Subscriptions subscriptions =
//                            JSONUtil.toBean(productBenefit.getBenefitDesc(), ProductCategoryDto.Subscriptions.class);
//                    subscriptions.setProductDescriptions(ProductDescriptions);
//                    subscriptionList.add(subscriptions);
//                }
//            }
//
//            productCategoryDto.setSubscriptions(subscriptionList);
//            return Response.ok(productCategoryDto);
//        }
//        return Response.error(TradeResultStatus.INVALID_PRODUCT_CATEGORY);
//    }

    /**
     * @param appCode         应用码
     * @param productCategory 产品类别
     * @return 产品类别全产品消息
     */
    @Override
    public Response<ProductCategoryDto> queryProductCategory(String appCode, Integer productCategory) {
        Map<String, Object> params = ServiceUtils.getParams();
        params.put("productCategory", productCategory);
        params.put("appCode", appCode);
        //查询产品类别消息
        List<ProductCategoryEntity> categoryList = productCategoryDao.selectByParams(params);
        if (CollUtil.isNotEmpty(categoryList)) {
            ProductCategoryEntity productCategoryEntity = categoryList.get(0);
            ProductCategoryDto productCategoryDto = productCategoryMapper.toDto(productCategoryEntity);

            //查询产品类别下全产品消息
            List<ProductDescriptionDto> productDescriptions =
                    PDSService.getProductDescriptions(appCode, productCategory);

            List<ProductCategoryDto.Subscriptions> subscriptionList =
                    PDSService.getSubscriptions(params, productDescriptions, Boolean.FALSE);

            productCategoryDto.setSubscriptions(subscriptionList);
            return Response.ok(productCategoryDto);
        }
        return Response.error(TradeResultStatus.INVALID_PRODUCT_CATEGORY);
    }


}
